﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;
using System.Web.UI.HtmlControls;

public partial class ProjectManage_TotalProject_Edit : System.Web.UI.Page
{
    TotalProjectBLL bll = new TotalProjectBLL();
    CommitmentBLL cbll = new CommitmentBLL();
    private string sid = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            sid = this.Request.QueryString["id"].ToString().Trim();
        }
        catch
        {

        }
        if (!IsPostBack)
        {
            bind();
            TotalProject tp = bll.GetModel(int.Parse(sid));
            ddltasktype.SelectedValue = tp.tasktype;
            ddlstage.SelectedValue = tp.stage;
            txttaskno.Text = tp.taskno;
            txtprojectno.Text = tp.projectno;
            txtname.Text = tp.name;
            ddlcommpany.SelectedValue = tp.commpany;
            ddlleaders.SelectedValue = tp.leaders;
            rblprojecttype.SelectedValue = tp.projecttype;
            rblprogresstype.SelectedValue = tp.progresstype;
            txtperson1.Text = tp.person1;
            txtperson2.Text = tp.person2;
            txtperson3.Text = tp.person3;
            txtplanner.Text = tp.planner;
            txttpdate.Text = tp.tpdate.ToString();
            txtbusiness.Text = tp.business;
            txtcomments1.Text = tp.comments1;
            txtcomments2.Text = tp.comments2;
            txtreviewedchange.Text = tp.reviewedchange;
            txtmeetingchange.Text = tp.meetingchange;
            txtremark.Text = tp.remark;
            txtcooperationunit.Text = tp.cooperationunit;
            txtcooperationperson.Text = tp.cooperationperson;
            List<Commitment> list = cbll.GetModelList("tp_id=" + sid);
            int i = 1;
            foreach (Commitment com in list)
            {
                HtmlSelect hs = (HtmlSelect)this.FindControl("sltprofessional" + i);
                hs.Value = com.professional;
                hs = (HtmlSelect)this.FindControl("sltdepartments" + i);
                hs.Value = com.departments.ToString();
                TextBox tb = (TextBox)this.FindControl("txtcompletiondate" + i);
                tb.Text = com.completiondate.ToString();
                tb = (TextBox)this.FindControl("txtdeliverydate" + i);
                tb.Text = com.deliverydate.ToString();
                tb = (TextBox)this.FindControl("txtreviewer" + i);
                tb.Text = com.reviewer.ToString();
                tb = (TextBox)this.FindControl("txtbranchreviewer" + i);
                tb.Text = com.branchreviewer.ToString();
                CheckBox cb = (CheckBox)this.FindControl("chkqualityplan" + i);
                cb.Text = com.qualityplan;
                cb = (CheckBox)this.FindControl("chkconferencereview" + i);
                cb.Text = com.conferencereview;
                list.Add(com);
                i++;
            }
        }
    }
    private void bind()
    {
        ddltasktype.DataSource = bll.getBaseData("2011");
        ddltasktype.DataTextField = "name";
        ddltasktype.DataValueField = "code";
        ddltasktype.DataBind();
        ddlstage.DataSource = bll.getBaseData("2012");
        ddlstage.DataTextField = "name";
        ddlstage.DataValueField = "code";
        ddlstage.DataBind();
        ddlcommpany.DataSource = bll.getBaseData("2013");
        ddlcommpany.DataTextField = "name";
        ddlcommpany.DataValueField = "code";
        ddlcommpany.DataBind();
        for (int i = 1; i <= 6; i++)
        {
            HtmlSelect hs = (HtmlSelect)this.FindControl("sltprofessional" + i);
            hs.DataSource = bll.getBaseData("2010");
            hs.DataTextField = "name";
            hs.DataValueField = "code";
            hs.DataBind();
            hs.Items.Insert(0, "");
            hs.Items[0].Value = "";
            hs = (HtmlSelect)this.FindControl("sltdepartments" + i);
            hs.DataSource = bll.getDept();
            hs.DataTextField = "name";
            hs.DataValueField = "id";
            hs.DataBind();
            hs.Items.Insert(0, "");
            hs.Items[0].Value = "";
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        TotalProject tp = new TotalProject();
        tp.tp_id = int.Parse(sid);
        tp.tasktype = ddltasktype.SelectedValue;
        tp.stage = ddlstage.SelectedValue;
        tp.taskno = txttaskno.Text.Trim();
        tp.projectno = txtprojectno.Text.Trim();
        tp.name = txtname.Text.Trim();
        tp.commpany = ddlcommpany.SelectedValue;
        tp.leaders = ddlleaders.SelectedValue;
        tp.projecttype = rblprojecttype.SelectedValue;
        tp.progresstype = rblprogresstype.SelectedValue;
        tp.person1 = txtperson1.Text;
        tp.person2 = txtperson2.Text;
        tp.person3 = txtperson3.Text;
        tp.planner = txtplanner.Text;
        tp.tpdate = DateTime.Parse(txttpdate.Text);
        tp.business = txtbusiness.Text;
        tp.comments1 = txtcomments1.Text;
        tp.comments2 = txtcomments2.Text;
        tp.reviewedchange = txtreviewedchange.Text;
        tp.meetingchange = txtmeetingchange.Text;
        tp.remark = txtremark.Text;
        tp.cooperationunit = txtcooperationunit.Text;
        tp.cooperationperson = txtcooperationperson.Text;
        List<Commitment> list = new List<Commitment>();
        for (int i = 1; i <= 6; i++)
        {
            Commitment com = new Commitment();
            HtmlSelect hs = (HtmlSelect)this.FindControl("sltprofessional" + i);
            com.professional = hs.Value;
            if (hs.Value != null)
            {
                hs = (HtmlSelect)this.FindControl("sltdepartments" + i);
                com.departments = int.Parse(hs.Value);
                TextBox tb = (TextBox)this.FindControl("txtcompletiondate" + i);
                com.completiondate = DateTime.Parse(tb.Text);
                tb = (TextBox)this.FindControl("txtdeliverydate" + i);
                com.deliverydate = DateTime.Parse(tb.Text);
                tb = (TextBox)this.FindControl("txtreviewer" + i);
                com.reviewer = int.Parse(tb.Text);
                tb = (TextBox)this.FindControl("txtbranchreviewer" + i);
                com.branchreviewer = int.Parse(tb.Text);
                CheckBox cb = (CheckBox)this.FindControl("chkqualityplan" + i);
                com.qualityplan = cb.Text;
                cb = (CheckBox)this.FindControl("chkconferencereview" + i);
                com.conferencereview = cb.Text;
                list.Add(com);
            }
        }
        bll.Update(tp, list);
    }
}
